<template>
	<view class="cart-popup">
		<view class="mask" @tap="handleClose"></view>
		<view class="content">
			<view class="header">
				<text class="title">购物车</text>
				<view class="clear" @tap="clearCart" v-if="cartList.length > 0">
					<image class="clear-icon" src="/static/menu/clear.png" />
					<text>清空</text>
				</view>
			</view>
			
			<view class="empty-cart" v-if="cartList.length === 0">
				<image class="empty-icon" src="/static/menu/empty-cart.png" />
				<text class="empty-text">购物车是空的</text>
				<view class="empty-tip">快去选购喜欢的商品吧~</view>
			</view>
			
			<scroll-view class="cart-list" scroll-y v-else>
				<view class="cart-item" v-for="(item, index) in cartList" :key="index">
					<image class="item-image" :src="item.image" mode="aspectFill" />
					<view class="item-info">
						<view class="item-header">
							<text class="item-name">{{item.name}}</text>
							<text class="item-specs">{{item.specs}}</text>
						</view>
						<view class="item-bottom">
							<text class="item-price">¥{{item.price}}</text>
							<view class="count-control">
								<view class="minus" @tap="decreaseCount(index)">-</view>
								<text class="count">{{item.count}}</text>
								<view class="plus" @tap="increaseCount(index)">+</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
export default {
	props: {
		cartList: {
			type: Array,
			default: () => []
		},
		totalPrice: {
			type: Number,
			default: 0
		}
	},
	methods: {
		handleClose() {
			this.$emit('close')
		},
		clearCart() {
			this.$emit('update', {
				list: [],
				total: 0,
				price: 0
			})
			this.handleClose()
		},
		decreaseCount(index) {
			const list = [...this.cartList]
			if (list[index].count > 1) {
				list[index].count--
				list[index].totalPrice = list[index].price * list[index].count
				this.updateCart(list)
			} else {
				// 数量为1时，询问是否删除
				uni.showModal({
					title: '提示',
					content: '是否删除该商品？',
					success: (res) => {
						if (res.confirm) {
							list.splice(index, 1)
							this.updateCart(list)
						}
					}
				})
			}
		},
		increaseCount(index) {
			const list = [...this.cartList]
			list[index].count++
			list[index].totalPrice = list[index].price * list[index].count
			this.updateCart(list)
		},
		updateCart(list) {
			const total = list.reduce((sum, item) => sum + item.count, 0)
			const price = list.reduce((sum, item) => sum + item.price * item.count, 0)
			this.$emit('update', { list, total, price })
		}
	}
}
</script>

<style lang="scss" scoped>
	.wp_-flex-row {
	  display: flex;
	  flex-direction: row;
	}
	
	.wp_-flex-col {
	  display: flex;
	  flex-direction: column;
	}
	
	.wp_-justify-start {
	  justify-content: flex-start;
	}
	
	.wp_-justify-end {
	  justify-content: flex-end;
	}
	
	.wp_-justify-center {
	  justify-content: center;
	}
	
	.wp_-justify-between {
	  justify-content: space-between;
	}
	
	.wp_-justify-around {
	  justify-content: space-around;
	}
	
	.wp_-justify-evenly {
	  justify-content: space-evenly;
	}
	
	.wp_-items-start {
	  align-items: flex-start;
	}
	
	.wp_-items-end {
	  align-items: flex-end;
	}
	
	.wp_-items-center {
	  align-items: center;
	}
	
	.wp_-items-baseline {
	  align-items: baseline;
	}
	
	.wp_-items-stretch {
	  align-items: stretch;
	}
	
	.wp_-self-start {
	  align-self: flex-start;
	}
	
	.wp_-self-end {
	  align-self: flex-end;
	}
	
	.wp_-self-center {
	  align-self: center;
	}
	
	.wp_-self-baseline {
	  align-self: baseline;
	}
	
	.wp_-self-stretch {
	  align-self: stretch;
	}
	
	.wp_-flex-1 {
	  flex: 1 1 0%;
	}
	
	.wp_-flex-auto {
	  flex: 1 1 auto;
	}
	
	.wp_-grow {
	  flex-grow: 1;
	}
	
	.wp_-grow-0 {
	  flex-grow: 0;
	}
	
	.wp_-shrink {
	  flex-shrink: 1;
	}
	
	.wp_-shrink-0 {
	  flex-shrink: 0;
	}
	
	.wp_-relative {
	  position: relative;
	}
.cart-popup {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	z-index: 999;
	
	.mask {
		position: absolute;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.6);
	}
	
	.content {
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #fff;
		border-radius: 40rpx 40rpx 0 0;
		padding: 40rpx;
		min-height: 400rpx;
		
		.header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 30rpx;
			
			.title {
				font-size: 32rpx;
				color: #333;
				font-weight: bold;
			}
			
			.clear {
				display: flex;
				align-items: center;
				
				.clear-icon {
					width: 32rpx;
					height: 32rpx;
					margin-right: 8rpx;
				}
				
				text {
					font-size: 26rpx;
					color: #999;
				}
			}
		}
		
		.empty-cart {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding: 60rpx 0;
			
			.empty-icon {
				width: 200rpx;
				height: 200rpx;
				margin-bottom: 30rpx;
			}
			
			.empty-text {
				font-size: 32rpx;
				color: #333;
				font-weight: 500;
				margin-bottom: 16rpx;
			}
			
			.empty-tip {
				font-size: 26rpx;
				color: #999;
			}
		}
		
		.cart-list {
			max-height: 60vh;
			
			.cart-item {
				display: flex;
				padding: 20rpx 0;
				border-bottom: 1rpx solid #f5f5f5;
				
				.item-image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 12rpx;
					margin-right: 20rpx;
				}
				
				.item-info {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					
					.item-header {
						.item-name {
							font-size: 28rpx;
							color: #333;
							font-weight: 500;
						}
						
						.item-specs {
							display: block;
							font-size: 24rpx;
							color: #999;
							margin-top: 8rpx;
						}
					}
					
					.item-bottom {
						display: flex;
						justify-content: space-between;
						align-items: center;
						margin-top: 20rpx;
						
						.item-price {
							font-size: 32rpx;
							color: #8cd548;
							font-weight: bold;
						}
						
						.count-control {
							display: flex;
							align-items: center;
							
							.minus, .plus {
								width: 48rpx;
								height: 48rpx;
								border-radius: 50%;
								background-color: #f5f5f5;
								display: flex;
								align-items: center;
								justify-content: center;
								font-size: 28rpx;
								color: #333;
							}
							
							.count {
								margin: 0 20rpx;
								font-size: 28rpx;
								color: #333;
							}
							
							.plus {
								background-color: #8cd548;
								color: #fff;
							}
						}
					}
				}
			}
		}
	}
}
</style>